using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Reflection;

namespace My.IdlConverter.Registration
{
    public class FreeDistribution : IFreeDistribution
    {
        private string _freeClass;

        public FreeDistribution()
        {
        }

        public string FreeClass 
        {
            get { return _freeClass; }
            set { _freeClass = value; }
        }

        public IList GetFree(int count)
        {
            using (ISession session = HibernateHelper.OpenSession())
            {
                using (ITransaction tx = session.BeginTransaction())
                {
                    IQuery query = session.CreateQuery(String.Format("from {0}", FreeClass));
                    query.SetMaxResults(count);
                    IList freeids = query.List();
                    tx.Commit();

                    return freeids;
                }
            }
        }

        public IList FindAll()
        {
            using (ISession session = HibernateHelper.OpenSession())
            {
                using (ITransaction tx = session.BeginTransaction())
                {
                    IQuery query = session.CreateQuery(String.Format("from {0}", FreeClass));
                    IList freeids = query.List();
                    tx.Commit();

                    return freeids;
                }
            }
        }

        public FreeId Find(Int64 id)
        {
            using (ISession session = HibernateHelper.OpenSession())
            {
                using (ITransaction tx = session.BeginTransaction())
                {
                    IQuery query = session.CreateQuery(String.Format("from {0} freeid where freeid.ID = :id", FreeClass));
                    query.SetInt64("id", id);
                    IEnumerator freeids = query.Enumerable().GetEnumerator();
                    FreeId freeid = null;
                    if (freeids.MoveNext())
                        freeid = freeids.Current as FreeId;
                    tx.Commit();

                    return freeid;
                }
            }
        }

        public void Remove(FreeId freeid)
        {
            using (ISession session = HibernateHelper.OpenSession())
            {
                using (ITransaction tx = session.BeginTransaction())
                {
                    FreeId findedFreeid = Find(freeid.ID);
                    if (findedFreeid != null)
                    {
                        findedFreeid.ID = freeid.ID;
                        session.Delete(findedFreeid);
                        tx.Commit();
                    }
                    else
                        throw new System.Exception("Remove Failed");
                }
            }
        }

        public void Update(FreeId freeid)
        {
            using (ISession session = HibernateHelper.OpenSession())
            {
                using (ITransaction tx = session.BeginTransaction())
                {
                    FreeId findedFreeid = Find(freeid.ID);
                    if (findedFreeid != null)
                    {
                        findedFreeid.ID = freeid.ID;
                        session.Update(findedFreeid);
                        tx.Commit();
                    }
                    else
                        throw new System.Exception("Update Failed");
                }
            }
        }

        public void Insert(FreeId freeid)
        {
            using (ISession session = HibernateHelper.OpenSession())
            {
                using (ITransaction tx = session.BeginTransaction())
                {
                    if (Find(freeid.ID) == null)
                    {
                        session.Save(freeid);
                        tx.Commit();
                        session.Close();
                    }
                    else
                    {
                        session.Close();
                        throw new Exception.DuplicateAddinException("The free id had been registered.");
                    }
                }
            }
        }
    }


}

